#!/bin/bash
# Copyright © 2021-2024 Jeffrey Cliff
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

TEMP=`mktemp`
TEMP1=`mktemp`

# echo $TEMP
grabLaundry.sh | sort | uniq > $TEMP1
cat $TEMP1 | grep -iv 'CST' > $TEMP
WHAT=`pullline.sh $TEMP`
DB=/home/themusicgod1/TDL/urls.db
FEDIDB=/home/themusicgod1/TDL/fedi.db
BASEDIR=/home/themusicgod1

# echo "are we hre"
echo "maximum" `grep -i "maximum" $BASEDIR/tools/grabLaundry.sh |  awk -F' ' '{print $8}' | awk -F')' '{print $1}'`
MAXLEVEL=`grep -i "maximum" $BASEDIR/tools/grabLaundry.sh |  awk -F' ' '{print $8}' | awk -F')' '{print $1}'`".0"
# MAXLEVEL="156.0"
echo "maxlevel" $MAXLEVEL

echo "what" $WHAT
FEDIYES=`echo $WHAT | grep -i '@.*@.*'`;
# echo $FEDIYES
#echo "fediyes" $FEDIYES;
if [ -z $FEDIYES ]; then
 echo "not fedi";
 sqlite3 $DB -line "select url, rss, other, id from news where url like \"%"$WHAT"%\"" | grep -i 'url' | awk -F' ' '{print $3}';

 SPC=`echo $WHAT | grep -i 'shitposter.world'`;
 echo $SPC
 if [[ -n $SPC ]]; then
  echo "spc";
#  echo "WHAT is " $WHAT
#  OUT2=`sqlite3 $DB -line "select level from news where url like \"%"$WHAT"%\"" | grep -i 'level'`
#  echo "OUT2:" $OUT2
  LEVEL=`sqlite3 $DB -line "select level from news where url like \"%"$WHAT"%\"" | grep -i 'level' | awk -F' ' '{print $3}'`
  DOMAIN="shitposter.world"
  LEVEL2=`sqlite3 $FEDIDB -line "select level from instances where name like \"%"$DOMAIN"%\"" | grep -i 'level'  | awk -F' ' '{print $3}'`
#grep  -i "shitposter.world" /home/themusicgod1/FediSus.html | head -n 1 | awk -F' ' '{print $2}'`

  echo "user Level is" $LEVEL2
  echo "LEVEL is " $LEVEL
  echo "we are over by:"
  echo "("$LEVEL2" - "$LEVEL")/ "$MAXLEVEL
  echo "("$LEVEL2" - "$LEVEL")/ "$MAXLEVEL  | bc
  howFar.sh $WHAT
  exit;
 fi

exit;
fi

echo "fedi";
DOMAIN2=`echo $WHAT | awk -F'@' '{print $3}'`;
#split off into its own script
$BASEDIR/tools/domain_estimate_bump.sh $DOMAIN2

# LEVEL2=`grep  -i $DOMAIN2 /home/themusicgod1/FediSus.html | head -n 1 | awk -F' ' '{print $2}'`
#echo "domain:"  $DOMAIN2
#echo "domain:"  $LEVEL2
#LEVEL=`sqlite3 $DB -line "select level from news where url like \"%"$WHAT"%\"" | grep -i 'level' | awk -F' ' '{print $3}'`
#echo "level:" $LEVEL
#echo "we are over by:"
#echo "("$LEVEL2" - "$LEVEL")/ "$MAXLEVEL
#echo "("$LEVEL2" - "$LEVEL")/ "$MAXLEVEL  | bc
howFar.sh $WHAT

FSEYES=`sqlite3 $DB -line "select url from news where url like \"%"$WHAT"%\"" | grep -i 'freespeechextremist'`;
if [[ -n $FSEYES ]]
 then
 echo "fse";
 WHAT2=`echo $WHAT | awk -F'@' '{print $2"@"$3}'`;
 echo "what2" $WHAT2;
 torify finger $WHAT2;
 echo $?;
else

sqlite3 $DB -line "select url, rss, other, id from news where url like \"%"$WHAT"%\"";

SPC=`sqlite3 /home/themusicgod1/TDL/urls.db -line "select url from news where url like \"%"$WHAT"%\"" | grep -i 'shitposter.world'`;
echo $SPC
if [[ -n $SPC ]]; then
 echo "spc";
 exit;
fi

 POAST=`echo $WHAT | grep -i 'poa.st'`;
 if [[ -n $POAST ]]; then
  echo "poast";
  LEVEL=`sqlite3 $DB -line "select level from news where url like \"%"$WHAT"%\"" | grep -i 'level' | awk -F' ' '{print $3}'`
  echo "LEVEL is " $LEVEL
  
#  LEVEL2=`grep  -i "poa.st" /home/themusicgod1/FediSus.html | head -n 1 | awk -F' ' '{print $2}'` 
  DOMAIN="poa.st"

# we're dealing with poast.
# we don't need the %
  LEVEL2=`sqlite3 $FEDIDB -line "select level from instances where name = \""$DOMAIN"\"" | grep -i 'level'  | awk -F' ' '{print $3}'`

  echo "LEVEL2 should be: " $LEVEL2
  echo "difference"
  echo "(" $LEVEL2" - "$LEVEL".0) / "$MAXLEVEL
  echo "(" $LEVEL2" - "$LEVEL".0) / "$MAXLEVEL | bc
  fi

 POAST=`echo $WHAT | grep -i 'gleasonator'`;
 if [[ -n $GLEASONATOR ]]; then
  echo "gleasonator";
  LEVEL=`sqlite3 $DB -line "select level from news where url like \"%"$WHAT"%\"" | grep -i 'level' | awk -F' ' '{print $3}'`
  echo "LEVEL is " $LEVEL
#  echo "should be: 382"
  #  echo "difference"
  DOMAIN="gleasonator.com"
  LEVEL2=`sqlite3 $FEDIDB -line "select level from instances where name like \"%"$DOMAIN"%\"" | grep -i 'level'  | awk -F' ' '{print $3}'`
#  LEVEL2=`grep  -i "gleasonator" /home/themusicgod1/FediSus.html | head -n 1 | awk -F' ' '{print $2}'`
  echo "should be: " $LEVEL2
  echo "difference"
  echo "(" $LEVEL2" - "$LEVEL".0) / "$MAXLEVEL
  echo "(" $LEVEL2" - "$LEVEL".0) / "$MAXLEVEL | bc
  #echo "(382.0 - "$LEVEL".0) / "$MAXLEVEL
#  echo "(382.0 - "$LEVEL".0) / "$MAXLEVEL | bc
 fi

fi

# this should already happen above. its either happening if its fedi or ifts not fedi
# GRAB SNOWFLAKE
#grabSnowflake.sh $WHAT
#echo "snowflake detection:"
#OUT=`sqlite3 $DB -line "select other from news where url like \"%"$WHAT"%\""| awk -F' ' '{print $3}'`;
#echo "other is:" $OUT
#if [[ -z $OUT ]]; then
#echo "blank other";
#ID=`sqlite3 /home/themusicgod1/TDL/urls.db -line "select id from news where url like \"%"$WHAT"%\"" | awk -F' ' '{print $3}'`;
#echo $ID;
#echo $WHAT;
#OTHER1=`sqlite3 $DB -line "select url, rss, other, id from news where url like \"%"$WHAT"%\"" | grep url | awk -F' ' '{print $3}'` ;
#OTHER2=`getSPCURL.sh $OTHER1`
#echo "update news set other = \""$OTHER2"\" where id = "$ID";"
#fi



